import com.sun.source.tree.Tree;//leecode 102

import java.util.*
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class LevelOrder {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ret=new ArrayList<>();//用二维顺序表存储每一层的数据，存放顺序表的顺序表
        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null) return ret;
        queue.offer(root);
        while(!queue.isEmpty()){
            List <Integer> tmp=new ArrayList<>();//存放每一层数据的顺序表
            int size=queue.size();
            while(size!=0){
                TreeNode cur=queue.poll();
                size--;
                tmp.add(cur.val);
                if(cur.left!=null){
                    queue.offer(cur.left);
                }
                if(cur.right!=null){
                    queue.offer(cur.right);
                }
            }
            ret.add(tmp);
        }
        return  ret;
    }
}
